package com.rookie.rookiemeeting;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

public class CodeGenerator {
    private static String url ="jdbc:mysql://47.92.157.229:3306/rookiemeeting?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8";
    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");
        String outputDir = projectPath+ "/src/main/java";

        String mapperPath = projectPath + "/src/main/resources/mapper";

        FastAutoGenerator.create(url, "root", "welLCcome1234")
            .globalConfig(builder -> {
                builder.author("henry-ong") // 设置作者
                        .enableSwagger() // 开启 swagger 模式
                        .disableOpenDir()
                        .outputDir(outputDir); // 指定输出目录
            })
            .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                if (typeCode == Types.SMALLINT) {
                    // 自定义类型转换
                    return DbColumnType.INTEGER;
                }
                return typeRegistry.getColumnType(metaInfo);

            }))
            .packageConfig(builder -> {
                builder.parent("com.rookie.rookiemeeting") // 设置父包名
                        .moduleName(null) // 设置父包模块名
                        .pathInfo(Collections.singletonMap(OutputFile.xml, mapperPath)); // 设置mapperXml生成路径
            })
            .strategyConfig(builder -> {
                builder.entityBuilder().enableLombok();  //lombok
                builder.controllerBuilder().enableHyphenStyle().enableRestStyle();
                builder.addInclude("mypass") // 设置需要生成的表名
                        .addTablePrefix("t_", "sys_"); // 设置过滤表前缀
            })
//            .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
            .execute();
    }
}
